import { execute } from "../../database/connector";

import { ProjectMembersQueries } from "../queries/project-members.query";
import { IProjectMembers } from "../../models/project-members.model";

/**
 * gets active projectMemberss
 */
export const getProjectMemberList = async () => {
  return execute<IProjectMembers[]>(ProjectMembersQueries.GetProjectMembers, []);
};

/**
 * gets a projectMembers based on id provided
 */
export const getProjectMembersById = async (id: IProjectMembers['id']) => {
  return execute<IProjectMembers[]>(ProjectMembersQueries.GetProjectMembersById, [id]);
};

/**
 * gets a projectMembers based on project id provided
 */
export const getProjectMembersByProjectId = async (projectId: IProjectMembers['projectId']) => {
  return execute<IProjectMembers[]>(ProjectMembersQueries.GetProjectMembersByProjectId, [projectId]);
};

/**
 * gets a projectMembers based on member id provided
 */
export const getProjectMembersByMemberId = async (memberId: IProjectMembers['projectId']) => {
  return execute<IProjectMembers[]>(ProjectMembersQueries.GetProjectsByMemberId, [memberId]);
};


/**
 * adds a new active projectMembers record
 */
export const insertProjectMembers = async (projectMembers: any[]) => {
  const result = await execute<{ affectedRows: number }>(ProjectMembersQueries.AddProjectMembers, [
    Object.values(projectMembers)
  ]);
  return result.affectedRows > 0;
};


/**
 * updates projectMembers information based on the id provided
 */
 export const deleteProjectMembersByProjectId = async (projectId: IProjectMembers['projectId']) => {
  const result = await execute<{ affectedRows: number }>(ProjectMembersQueries.DeleteProjectMembersByProjectId, [
    projectId
  ]);
  return result.affectedRows > 0;
};